package com.yue.yadmin.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;


public class Result extends HashMap<String, Object> {
	private static final long serialVersionUID = -14;
	
	private static final String DEFAULTKEY = "result";
	
	private static final String PAGE_LIST = "data";
	private static final String PAGE_COUNT = "recordsTotal";
	private static final String PAGE_FILTERED = "recordsFiltered";
	
	public Result() {
		put("code", 0);
	}
	
	public Result(int code,String msg) {
		put("code", 0);
		put("msg", 0);
	}
	
	public String toJson(){
		return JSONObject.toJSONString(this);
	}
	
	public static Result error() {
		return error(-1, "未知异常，请联系管理员");
	}
	
	public static Result error(String msg) {
		return error(-1, msg);
	}
	
	public static Result error(int code, String msg) {
		Result r = new Result();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static Result ok(String msg) {
		Result r = new Result();
		r.put("msg", msg);
		return r;
	}
	
	public static Result ok(Map<String, Object> map) {
		Result r = new Result();
		r.putAll(map);
		return r;
	}
	
	public static Result ok() {
		return new Result();
	}
	
	public static Result page(PageInfo<?> pageInfo) {
		Result r = new Result();
		r.put(PAGE_LIST, pageInfo.getList());
		r.put(PAGE_COUNT, pageInfo.getTotal());
		r.put(PAGE_FILTERED, pageInfo.getTotal());
		return r;
	}

	public Result put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	public Result put( Object value) {
		super.put(DEFAULTKEY, value);
		return this;
	}
    
}
